package com.o2o.service;

import java.io.IOException;
import java.util.List;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.o2o.base.BaseService;
import com.o2o.model.topic.Category;
import com.o2o.model.user.User;
import com.o2o.model.user.UserProfile;

@Scope("prototype")
@Service
public class UserService extends BaseService{
	/**
	 * 根据用户email查找User对象
	 * @param emailAccount 用户email
	 * @return User唯一对象
	 */
	public User getUserByEmailAccount(String emailAccount){
		return userRepository.getUserByEmailAccount(emailAccount);
	}
	/**
	 * 根据用户注册名查找User对象
	 * @param userName 用户注册公司名
	 * @return 唯一User对象
	 */
	public User getUserByUserName(String userName){
		return userRepository.getUserByUserName(userName);
	}
	
	/**
	 * 根据UserId查找用户详细资料
	 * @param userId 用户id
	 * @return UserProfile 用户详细资料
	 */
	public UserProfile getUserProfileByUserId(long userId){
		return userRepository.getUserProfileByUserId(userId);
	}
	/**
	 * 保存或更新UserProfile用户详细资料
	 * @param userName 用户名字
	 * @param phoneNumber 电话号码
	 * @param userEmail email
	 * @param userId 用户id
	 * @param fullAddressDetail 用户全地址
	 * @param areaCode 区域code
	 * @param address 街道或者小区
	 * @return true/false
	 */
	public boolean saveOrUpdateUserProfile(String userName,String phoneNumber,String userEmail,long userId,String fullAddressDetail,int areaCode,String address){
		return userRepository.saveOrUpdateUserProfile(userName, phoneNumber, userEmail, userId, fullAddressDetail, areaCode, address);
	}
	
	
	
	public List<Category> getAllCategorys(int status){
		return userRepository.getAllCategorys(status);
	}
	/**
	 * 保存或更新User用户,只更新passwd字段,其他字段会忽略
	 * @param name 注册名
	 * @param picture 用户头像
	 * @param passwd 密码
	 * @param status 帐号状态
	 * @param emailAccount email登录账户
	 * @return true/false
	 * @throws IOException 
	 */
	public boolean saveOrUpdateUser(final String name,final String passwd,final String emailAccount,final int status,final String picture) throws IOException{
		long time = System.currentTimeMillis();
		long uid = userRepository.saveOrUpdateUser(name, passwd, status, picture, emailAccount, time);
		if(uid > 0){
//			hBaseServiceRepository.saveAndUpdateUser(uid, name, passwd, emailAccount, status, picture, time);
			return true;
		}else return false;
	}
}
